<?

require_once(dirname(__FILE__) . '/../config.php');

$admin_password = filter_input(INPUT_COOKIE, 'admin_password', FILTER_SANITIZE_STRING);

if ($admin_password != BSDDS_ADMIN_PASSWORD_HASHED) {
	header("Location: " . WEB_ROOT . 'admin/login.php');
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Transaction History</title>	
		<style type="text/css">	
			body, p, td {
				font-size: 12px;
				font-family: arial;
			}
	
			table {
			}
	
		</style>	
	</head>
	<body>

<?
require_once(APPLICATION_ROOT . "/db.php");

$query = $sql->query("SELECT * FROM " . BSDDS_PURCHASE_TABLE . " ORDER BY transactionID DESC");
$info = mysql_fetch_array($query);

print <<<TABLE

		<h1>Transaction History</h1>

		<table border="1" summary="Transactions" width="1300">
			<tr>
				<td>ID</td>
				<td>Item #</td>
				<td>Name</td>
				<td>Type</td>
				<td>Price</td>
				<td>Currency</td>
				<td>Format</td>
				<td>Buyer</td>
				<td>Buyer ID</td>
				<td>Buyer Email</td>
				<td>Time</td>
				<td>Download Time Remaining</td>
				<td>Status</td>
				<td>Transaction ID</td>
			</tr>
TABLE;

while ($info) {
	$date = date("n-d-y h:ia", strtotime($info[transaction_time]));
	if ($info['status'] == "Completed") {
		$otherstatus = "Pending";
	} else {
		$otherstatus = "Completed";
	}

	# Remaining time.
	$temp_time = strtotime($info[time]) + BSDDS_LINK_EXPIRY;
	$remaining_time = round((($temp_time - time()) / 60));

	if ($remaining_time > 0) {
		$remaining_time .= " minutes";
	} else {
		$remaining_time = "expired";
	}

	$transactionID = htmlentities($info[transactionID]);
	$transactionID_url = urlencode($info[transactionID]);
	$itemID = htmlentities($info[itemID]);
	$name = htmlentities($info[name]);
	$type = htmlentities($info[type]);
	$price = htmlentities($info[price]);
	$currency = htmlentities($info[currency]);
	$format = htmlentities($info[format]);
	$buyer = htmlentities($info[buyer]);
	$buyer_id = htmlentities($info[buyer_id]);
	$email = htmlentities($info[email]);
	$status = htmlentities($info[status]);
	$txn_id = htmlentities($info[txn_id]);

	print <<<TR
	
			<tr>
				<td>{$transactionID}</td>
				<td>{$itemID}</td>
				<td>{$name}</td>
				<td>{$type}</td>
				<td>{$price}</td>
				<td>{$currency}</td>
				<td>{$format}</td>
				<td>{$buyer}</td>
TR;

echo '				<td><a href="' . WEB_ROOT . '?userid=' . $buyer_id . '">' . $buyer_id . '</a></td>';
print <<<TR

				<td><a href="mailto:{$email}">{$email}</a></td>
				<td>$date</td>
				<td>$remaining_time</td>
				<td><a href="setstatus.php?id={$transactionID_url}&status=$otherstatus">{$status}</a></td>
				<td>{$txn_id}</td>
			</tr>
TR;

	$totalprice = $totalprice + $info[price];
	$totalitems = $totalitems + 1;

	$info = mysql_fetch_array($query);
}

print <<<TABLE
		</table>
TABLE;

print <<<TOTALS
		<p style="font-size: 20px;">
			Total Items: $totalitems<br>
			Total Gross: $$totalprice<br>
		</p>
TOTALS;

?>

	</body>
</html>